<template>
  <el-card>
    <h2>客房类型管理</h2>
    <el-button type="primary" @click="openAdd">新增类型</el-button>
    <el-table :data="roomTypes" style="width: 100%;margin-top:16px">
      <el-table-column prop="typeId" label="ID" width="60" />
      <el-table-column prop="typeName" label="类型名称" />
      <el-table-column prop="basePrice" label="基础价格" />
      <el-table-column prop="capacity" label="容量" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDelete(scope.row.typeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" />
        </el-form-item>
        <el-form-item label="基础价格" prop="basePrice">
          <el-input v-model="form.basePrice" type="number" />
        </el-form-item>
        <el-form-item label="容量" prop="capacity">
          <el-input v-model="form.capacity" type="number" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getRoomTypes, addRoomType, updateRoomType, deleteRoomType } from '../api/roomType';
import { ElMessage, ElMessageBox } from 'element-plus';

const roomTypes = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const form = ref({ typeId: null, typeName: '', basePrice: '', capacity: '', description: '' });
const formRef = ref();

const rules = {
  typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
  basePrice: [{ required: true, message: '请输入基础价格', trigger: 'blur' }],
  capacity: [{ required: true, message: '请输入容量', trigger: 'blur' }]
};

const loadRoomTypes = async () => {
  roomTypes.value = await getRoomTypes();
};

const openAdd = () => {
  dialogTitle.value = '新增类型';
  form.value = { typeId: null, typeName: '', basePrice: '', capacity: '', description: '' };
  dialogVisible.value = true;
};

const openEdit = (row) => {
  dialogTitle.value = '编辑类型';
  form.value = { ...row };
  dialogVisible.value = true;
};

const onSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return;
    if (!form.value.typeId) {
      await addRoomType(form.value);
      ElMessage.success('新增成功');
    } else {
      await updateRoomType(form.value);
      ElMessage.success('修改成功');
    }
    dialogVisible.value = false;
    loadRoomTypes();
  });
};

const onDelete = (id) => {
  ElMessageBox.confirm('确定要删除该类型吗？', '提示', { type: 'warning' })
    .then(async () => {
      await deleteRoomType(id);
      ElMessage.success('删除成功');
      loadRoomTypes();
    });
};

onMounted(loadRoomTypes);
</script> 